package com.jeecms.wicool.manager.car.impl;



import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.car.UsedCarDao;
import com.jeecms.wicool.entity.car.CarBrand;
import com.jeecms.wicool.entity.car.CarSeries;
import com.jeecms.wicool.entity.car.CarType;
import com.jeecms.wicool.entity.car.UsedCar;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.manager.car.CarBrandMng;
import com.jeecms.wicool.manager.car.CarSeriesMng;
import com.jeecms.wicool.manager.car.CarTypeMng;
import com.jeecms.wicool.manager.car.UsedCarMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;

@Service
@Transactional
public class UsedCarMngImpl implements UsedCarMng {
	
	private UsedCarDao dao;
	@Autowired
	public void setDao(UsedCarDao dao) {
		this.dao = dao;
	}
	
	@Transactional(readOnly = true)
	public Pagination getPage(String title,Integer carBrandId,Integer carSeriesId,Integer carTypeId ,int pageNo, int pageSize) {
		Pagination page = dao.getPage(title, carBrandId, carSeriesId, carTypeId, pageNo, pageSize);
		return page;
	}
	@Override
	public UsedCar findById(Integer id) {
		UsedCar entity = dao.findById(id);
		return entity;
	}
	@Override
	public List<UsedCar> findByCarTypeId(Integer carTypeId){
		List<UsedCar> list=dao.findByCarTypeId(carTypeId);
		return list;
	}
	@Override
	public UsedCar update(UsedCar bean,Integer shopId,Integer carBrandId,Integer carSeriesId,Integer carTypeId) {
		ShopInfo shop = shopMng.findById(shopId);
		CarBrand carBrand=carBrandMng.findById(carBrandId);
		CarSeries carSeries=carSeriesMng.findById(carSeriesId);
		CarType carType=carTypeMng.findById(carTypeId);
		bean.setShop(shop);
		bean.setCarBrand(carBrand);
		bean.setCarSeries(carSeries);
		bean.setCarType(carType);
		Updater<UsedCar> updater = new Updater<UsedCar>(bean);
		bean = dao.updateByUpdater(updater);
		return bean;
	}

	@Override
	public UsedCar save(UsedCar bean,Integer shopId,Integer carBrandId,Integer carSeriesId,Integer carTypeId) {
		ShopInfo shop = shopMng.findById(shopId);
		CarBrand carBrand;
		if(carBrandId==0){
			 carBrand=new CarBrand();
			 carBrand.setId(carBrandId);
		}else{
			 carBrand=carBrandMng.findById(carBrandId);
		}
		
		CarSeries carSeries;
		if(carSeriesId==0){
			carSeries = new CarSeries();
			carSeries.setId(carSeriesId);
		}else{
			carSeries=  carSeriesMng.findById(carSeriesId);
		}
		CarType carType;
		if(carTypeId==0){
			carType=new CarType();
			carType.setId(carTypeId);
		}else{
			carType=carTypeMng.findById(carTypeId);
		}
		bean.setShop(shop);
		bean.setCarBrand(carBrand);
		bean.setCarSeries(carSeries);
		bean.setCarType(carType);
		dao.save(bean);
		return bean;
	}

	@Override
	public UsedCar deleteById(Integer id) {
		UsedCar bean = dao.deleteById(id);
		return bean;
	}
	@Override
	public UsedCar[] deleteByIds(Integer[] ids) {
		UsedCar[] beans = new UsedCar[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}
	@Autowired
	private WiShopInfoMng shopMng;
	@Autowired
	private CarBrandMng carBrandMng;
	@Autowired
	private CarSeriesMng carSeriesMng;
	@Autowired
	private CarTypeMng carTypeMng;
}